package org.eweb4j.solidbase.user.web;

import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;

import org.eweb4j.solidbase.user.model.User;
import org.eweb4j.solidbase.user.model.UserCons;
import org.eweb4j.util.StringUtil;

import com.google.code.kaptcha.Constants;

@Path("${UserConstant.MODEL_NAME}")
public class LoginAction extends BaseAction {

	@GET
	@Path("login")
	public String login() {
		return UserCons.LOGIN_ACTION_RESULT();
	}

	@PUT
	@Path("login")
	public String doLogin() {
		try {
			String _authCode = (String) session
					.getAttribute(Constants.KAPTCHA_SESSION_KEY);
			User loginUser = userService.login(_authCode,
					StringUtil.getIpAddr(request), user);
			session.setAttribute(UserCons.LOGIN_USER_ATTR_NAME(), loginUser);
		} catch (Exception e) {
			request.setAttribute(UserCons.LOGIN_ERR_ATTR_NAME(), e.getMessage());
			return UserCons.LOGIN_ACTION_RESULT();
		}

		return UserCons.LOGIN_SUCCESS_REDIRECT();
	}

}
